package com.proj.hot.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.proj.hot.GlobalParams;
import com.proj.hot.R;
import com.proj.hot.utils.DebugUtil;

import org.json.JSONObject;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * 商品详情页
 */
public class GoodsDetailActivity extends BaseActivity {

    private ImageView goodsImg;//商品图片
    private TextView goodsTextName;//商品名称
    private TextView goodsTextNum;//商品编号
    private TextView goodsTextprice;//商品价格
    private TextView goodsTextphone;//商家电话

    private ImageView goodsImgCode;//商品二维码
    private TextView goodsTextCodeTag;//提示语：微信支付或者支付宝支付

private TextView gw;

    String contentid;
    boolean isPay=false;
    boolean isfinish=false;


    private Handler handler =  new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what == 0){

                if(isPay) {
                    Toast.makeText(GoodsDetailActivity.this, "支付成功！", Toast.LENGTH_LONG).show();
                    finish();
                }else{
                    if(!isfinish){
                        getIsPay();
                        handler.sendEmptyMessageDelayed(0,3000);
                    }

                }
            }
        }
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goodsdetail);
        gw = (TextView)this.findViewById(R.id.gw);
        contentid=getIntent().getStringExtra("contentid");

        if (GlobalParams.screenHeight == 1080){
            gw.setTextSize(25);
        }
        gw.setText("<  "+"购物");
        initView();
        getContent();
    }
    private void initView(){
        goodsImg = (ImageView)this.findViewById(R.id.goods_img);
        goodsTextName = (TextView)this.findViewById(R.id.goods_text_name);
        goodsTextNum = (TextView)this.findViewById(R.id.goods_text_num);
        goodsTextprice = (TextView)this.findViewById(R.id.goods_text_price);
        goodsTextphone = (TextView)this.findViewById(R.id.goods_text_phone);
        goodsImgCode = (ImageView)this.findViewById(R.id.goods_img_code);
        goodsTextCodeTag = (TextView)this.findViewById(R.id.goods_text_wxorali);
    }










    @Override
    protected void onDestroy() {
        super.onDestroy();
        isfinish=true;
    }

    @Override
    protected void onPause() {
        super.onPause();
        isfinish=true;
    }

    private void getIsPay(){

        try {

            JSONObject body = new JSONObject();
            body.put("sn", GlobalParams.SN);
            body.put("contentid", contentid);
            body.put("accountid", GlobalParams.user_id);


            RequestParams params = new RequestParams(GlobalParams.reqHost + "attestation/ispay");
            params.setAsJsonContent(true);
            params.setBodyContent(body.toString().trim());

            /**
             * ---------------send------------------
             */
            x.http().post(params, new org.xutils.common.Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String s) {
                    try {
                        JSONObject jData=new JSONObject(s);

                        JSONObject respbody=jData.getJSONObject("respbody");
                        JSONObject data=respbody.getJSONObject("data");
                        if (data.getInt("ispay")==1){
                            isPay=true;
                        }


                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }

                @Override
                public void onFinished() {

                }

                @Override
                public void onCancelled(CancelledException e) {

                }

                @Override
                public void onError(Throwable throwable, boolean b) {

                }
            });


        }catch (Exception e){
            e.printStackTrace();
        }

    }






    private void getContent(){

        try {

            JSONObject body = new JSONObject();
            body.put("sn", GlobalParams.SN);
            body.put("contentid", contentid);
            //body.put("contentid", "26");
            body.put("accountid", GlobalParams.user_id);
            body.put("mac", GlobalParams.MAC_ADDRESS);


            RequestParams params = new RequestParams(GlobalParams.reqHost + "content/getContent");
            params.setAsJsonContent(true);
            params.setBodyContent(body.toString().trim());

            /**
             * ---------------send------------------
             */
            x.http().post(params, new org.xutils.common.Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String s) {
                    try {
                        DebugUtil.i("gggg",s);
                        JSONObject jData=new JSONObject(s);
                        JSONObject respbody=jData.getJSONObject("respbody");
                        JSONObject data=respbody.getJSONObject("data");
                        String outsidurl=data.getString("outsidurl");
                        String phone=data.getString("phone");
                        String price=data.getString("price");
                        String proid=data.getString("proid");
                        String title=data.getString("title");
                        String logo=data.getString("logo");

                        ImageLoader.getInstance().displayImage(logo,goodsImg);
                        ImageLoader.getInstance().displayImage(outsidurl,goodsImgCode);

                        goodsTextprice.setText("￥"+price);
                        goodsTextphone.setText(phone);
                        goodsTextName.setText(title);
                        goodsTextNum.setText(proid);




                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }

                @Override
                public void onFinished() {

                }

                @Override
                public void onCancelled(CancelledException e) {

                }

                @Override
                public void onError(Throwable throwable, boolean b) {

                }
            });


        }catch (Exception e){
            e.printStackTrace();
        }

    }





}
